<?php
/**
 * lemocms
 * ============================================================================
 * 版权所有 2018-2027 lemocms，并保留所有权利。
 * 网站地址: https://www.lemocms.com
 * ----------------------------------------------------------------------------
 * 采用最新Thinkphp6实现
 * ============================================================================
 * Author: yuege
 * Date: 2019/9/4
 */

namespace app\wechat\controller;

use app\common\controller\Base;

use app\common\model\WxMaterial;
use app\common\model\WxMaterialInfo;
use app\common\model\WxMsgHistory;
use app\common\model\WxReply;
use EasyWeChat\Kernel\Messages\Image;
use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Kernel\Messages\NewsItem;
use EasyWeChat\Kernel\Messages\Video;
use EasyWeChat\Kernel\Messages\Voice;

use think\Exception;
use think\facade\Db;
use think\facade\Request;
use util\WechatApp;

class WechatApi extends Base
{
    protected $where =[];
    protected $wechatApp;
    protected $store_id;
    protected $wx_aid;
    public function initialize()
    {

        parent::initialize(); // TODO: Change the autogenerated stub
        $wechatApp = new WechatApp();
        $this->wechatApp = $wechatApp->wechatApp;
        $this->store_id =  $wechatApp->store_id;
        $this->wx_aid =  $wechatApp->wechat->id;
        $this->where= ['store_id'=>$this->store_id,'wx_aid'=>$this->wx_aid];

        $this->getMessage();

    }

    /**
     * @throws \EasyWeChat\Kernel\Exceptions\BadRequestException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * 微信开启服务器关联
     */
    public function related()
    {

        $response = $this->wechatApp->server->serve();
        // 将响应输出
        $response->send();  exit;

    }

    /*
     * 接受消息
     *
     */
    public function getMessage()
    {

        $this->wechatApp->server->push(function ($message) {
            $file = './log.txt';
            if (!$file) {
                mkdir('./log.txt', '777', true);
            }
            $files = fopen($file, 'a+');
            fwrite($files, json_encode($message));
            try{
                $this->addMsg($message);

            }catch (\Exception $e){
                throw new Exception($e->getMessage());
            }

            switch ($message['MsgType']) {
                case 'event':
                    $result = $this->MsgTypeEvent($message);
                    break;
                case 'text':
                    $result = $this->MsgTypeText($message);
                    break;
                case 'image':
                    $result = $this->MsgTypeImage($message);
                    break;
                case 'voice':
                    $result = $this->MsgTypeVoice($message);
                    break;
                case 'video':
                    $result = $this->MsgTypeVideo($message);
                    break;
                case 'location':
                    $result = $this->MsgTypeLocation($message);
                    break;
                case 'link':
                    $result = $this->MsgTypeLink($message);
                    break;
                case 'file':
                    $result = $this->MsgTypeFile($message);
                    break;
                // ... 其它消息
                default:
                    $result = '欢迎关注！';
                    break;
            }

            return $result;


        });
    }

    /**
     * *****************消息事件*************************************************
     */
    public function MsgTypeEvent($message)
    {

        $contentStr = "";
        switch ($message['Event']) {
            case "subscribe": // 关注公众号 添加关注回复
                $content = $this->getSubscribeReply($message);
                // 构造Material数据并返回
                break;
            case "unsubscribe": // 取消关注公众号
                $content = $this->getSubscribeReply($message);
                break;
            case "VIEW": // VIEW事件 - 点击菜单跳转链接时的事件推送
                $content = "";
                break;
            case "SCAN": // SCAN事件 - 用户已关注时的事件推送
                $content = "";
                break;
            case "CLICK": // CLICK事件 - 自定义菜单事件

                break;
            default:
                break;
        }

        return $content;
    }

    /**
     * @param $message
     * @return int
     * 文本
     */
    public function MsgTypeText($message)
    {
        $content = $message['Content'];
        $info = WxReply::where($this->where)->where('keyword','like','%'.$content.'%')->find();

        if($info){
            $content =  $this->getWeixinReplyDetail($info);
        }else{
            $content =  $this->getWeixinReplyDefault();
        }
        return $content;

    }

    public function MsgTypeImage($message)
    {

        return $this->getWeixinReplyDefault();
    }

    /*
     * 音频
     */
    public function MsgTypeVoice($message)
    {

        return $this->getWeixinReplyDefault();

    }

    /**
     * @param $message
     * 视频
     */
    public function MsgTypeVideo($message)
    {

        return $this->getWeixinReplyDefault();
    }

    /**
     * @param $message
     * 定位
     */
    public function MsgTypeLocation($message)
    {
        return $this->getWeixinReplyDefault();

    }

    /**
     * @param $message 链接
     */
    public function MsgTypeLink($message)
    {
        return $this->getWeixinReplyDefault();
    }

    public function MsgTypeFile($message)
    {
        return $this->getWeixinReplyDefault();

    }


    /**
     * *****************获取消息*************************************************
     */
    /**
     * 获取关注回复
     * @param
     * @return unknown|string
     */
    public function getSubscribeReply()
    {
        $weixin_replay = Db::name('wx_replay');
        $info = $weixin_replay
            ->where($this->where)
            ->where('type', 'subscribe')
            ->find();
        if (!empty($info)) {
            $info = $this->getWeixinReplyDetail($info);
            return $info;
        } else {
            return $this->getWeixinReplyDefault();
        }
    }

    /**
     *
     *
     */

    protected function getWeixinReplyDefault(){

        $message = WxReply::where($this->where)->where('type','default')->value('data');
        return $message;

    }
    /**
     * @param $Material_id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 媒体详情
     */

    protected function getWeixinReplyDetail($info)
    {
        $msg_type = $info->msg_type;
        $material_id = $info->material_id;
        $media_id = '';
        if($material_id){
            $material = WxMaterial::find($material_id);
            if($material){
                $media_id = $material->media_id;

            }
        }
        $message = WxReply::where($this->where)->where('type','default')->value('data');

        switch ($msg_type) {
            case 'text':
                $message = $info->data;
                break;

            case 'image':
                if(WxMaterial::where('media_id',$media_id)->find()){

                    $message =  new Image($media_id);
                }
                break;

            case 'news':
                $new = WxMaterialInfo::where($this->where)->where('material_id', $material_id)->find();

                if ($new) {
                    $newsList[] = new NewsItem([
                        'title' => $new->title,
                        'description' => $new->digest,
                        'url' => $new->url,
                        'image' => $new->cover,
                    ]);
                    $message = new News($newsList);
                }
                break;
            case 'video':
                if(WxMaterial::where('media_id',$media_id)->find()) {

                    $message =  new Video($media_id, $material->file_name, $material->des);
                }

                break;
            case 'voice':
                if(WxMaterial::where('media_id',$media_id)->find()) {

                    $message =  new Voice($media_id);
                }
                break;
            default:
                break;
        }

        return $message;

    }


    //添加消息
    public function addMsg($message){

        $type = $message['MsgType'];
        $data['store_id'] = $this->store_id;
        $data['wx_aid'] = $this->wx_aid;
        $data['openid'] = $message['FromUserName'];
        $data['nickname'] = $this->wechatApp->user->get($message['FromUserName'])['nickname'];
        $data['content_json'] = json_encode($message);
        $data['create_time'] = $message['CreateTime'];
        $data['status'] = 1;
        $data['type'] = $type;
        switch ($type){
            case 'text':
                $data['content'] = $message['Content'];
                $keys = WxReply::where($this->where)
                    ->where('keyword','like',$data['content'])
                    ->find();
                if($keys){
                    $data['keyword_id'] = $keys->id;
                }
                break;
            case 'image':
                $data['content'] = $message['PicUrl'];
                $data['Material_id'] = $message['MediaId'];
                break;
            case 'voice':
                $data['content'] = $data['content_json'];
                $data['Material_id'] = $message['MediaId'];
                break;
            case 'video':
                $data['content'] = $data['content_json'];
                $data['Material_id'] = $message['MediaId'];
                break;
            case 'shortvideo':
                $data['content'] = $data['content_json'];
                $data['Material_id'] = $message['MediaId'];
                break;
            case 'location':
                $data['content'] = $data['content_json'];
                break;
            case 'link':
                $data['content'] = $data['content_json'];
                break;
            case 'event';
                $data['event'] = $message['Event'];
                break;
            default:
                $data['content'] = $message['Content'];
                $keys = WxReply::where($this->where)
                    ->where('keyword','like',$data['content'])
                    ->find();
                if($keys){
                    $data['keyword_id'] = $keys->id;
                }
                break;




        }
        $res = WxMsgHistory::create($data);
        if($res){
            return $res;
        }else{
            return '';
        }
    }

}